from PyQt5.QtWidgets import (QApplication, QButtonGroup, QRadioButton, QCheckBox, QComboBox,QTabWidget,QMessageBox,QTextEdit,QFrame,QAction,
                             QWidget, QLineEdit, QLabel, QPushButton, QFileDialog, QHBoxLayout, QGridLayout, QVBoxLayout, QListView)
from PyQt5.QtCore import Qt, QTimer, QBasicTimer, QDate, QAbstractListModel, QStringListModel, QEvent,pyqtSignal,QObject
from PyQt5.QtGui import QPixmap,QColor,QIcon,QMouseEvent

import sys

from configUtils import *

class CustomSignal(QObject): # 自定义信号槽
    closed = pyqtSignal([bool])

class SettingWin(QWidget):
    def __init__(self,parent:QWidget=None):
        super().__init__(parent,Qt.Window|Qt.MSWindowsFixedSizeDialogHint )
        self.c = CustomSignal()
        self.closed = self.c.closed 
       
        self.resize(400,300)
        self.setWindowModality(Qt.ApplicationModal) #模态化窗口 
        self.setWindowTitle("设置")
        lbl = QLabel(self)
        lbl.setText("类作者:")
        lbl.resize(50,30)
        lbl.setAlignment(Qt.AlignCenter)
        lbl.move(30,30)
        gap = 5
        self.authorTxt = QLineEdit(self)        
        self.authorTxt.move(lbl.x()+lbl.width(),lbl.y()+gap)
        self.authorTxt.textChanged.connect(self.onAuthorChange)
        
        lblName = QLabel(self)        
        lblName.setText("字段属性:")
        lblName.adjustSize() #自动大小,需要先设置text再重新渲染
        lblName.move(lbl.x(),self.authorTxt.y()+self.authorTxt.height()+gap)

        privateBtn = self.getRadioBtn("private",lblName.x()+lblName.width()+gap,lblName.y())        
        protectedBtn = self.getRadioBtn("protected",privateBtn.x()+privateBtn.width(),privateBtn.y())
        publicBtn = self.getRadioBtn("public",protectedBtn.x()+protectedBtn.width(),protectedBtn.y())

        # 字段限定名 按钮组
        self.varProGroup = QButtonGroup()
        self.varProGroup.addButton(privateBtn)
        self.varProGroup.addButton(protectedBtn)
        self.varProGroup.addButton(publicBtn)
        self.varProGroup.buttonClicked.connect(self.onRadioBtnClick)
        gap = 15
        self.autoBtnCB = self.getCheckBox("自动按钮事件",lbl.x(),publicBtn.y()+publicBtn.height()+gap)
        self.autoListCb = self.getCheckBox("自动List事件",self.autoBtnCB.x()+self.autoBtnCB.width()+gap,self.autoBtnCB.y())
        self.autoCtrlCb = self.getCheckBox("自动controller事件",self.autoListCb.x()+self.autoListCb.width()+gap,self.autoBtnCB.y())        
        self.fieldStyleCb = self.getCheckBox("驼峰变量名",lbl.x(),self.autoCtrlCb.y()+self.autoCtrlCb.height()+gap)
        autoTips = "在创建baseModule或者模块文件夹时自动检测创建"
        self.mkProxyCb = self.getCheckBox("自动创建proxy类",self.fieldStyleCb.x()+self.fieldStyleCb.width(),self.fieldStyleCb.y(),autoTips)
        self.mkCtrlCb = self.getCheckBox("自动创建controller类",self.mkProxyCb.x()+self.mkProxyCb.width(),self.mkProxyCb.y(),autoTips)
        self.mkCacheCb = self.getCheckBox("自动创建cache类",self.fieldStyleCb.x(),self.mkCtrlCb.y()+self.mkCtrlCb.height()+gap,autoTips)

        self.autoBtnCB.clicked.connect(self.onCbClick)
        self.autoListCb.clicked.connect(self.onCbClick)
        self.autoCtrlCb.clicked.connect(self.onCbClick)
        self.fieldStyleCb.clicked.connect(self.onCbClick)

        self.saveBtn = QPushButton(self)
        self.saveBtn.setText("保存")
        px = (self.width() - self.saveBtn.width())/2
        py = self.height()-self.saveBtn.height()-5        
        self.saveBtn.move(px,py)
        self.saveBtn.clicked.connect(self.onClickSave)
        self.isChange = False
        self.updateSetting()

    def updateSetting(self):
        self.authorTxt.setText(getToolCfg(EToolCfg.author.name))
        index = getToolCfg(EToolCfg.fieldPro.name)
        btns = self.varProGroup.buttons()
        index = min(index,len(btns)-1)
        self.proBtn = protectedBtn = btns[index]
        self.proBtn.setChecked(True)

        flag = bool(getToolCfg(EToolCfg.autoBtn.name))
        self.autoBtnCB.setChecked(flag)

        flag = bool(getToolCfg(EToolCfg.autoList.name))
        self.autoListCb.setChecked(flag)
        
        flag = bool(getToolCfg(EToolCfg.autoMkProxy.name))
        self.mkProxyCb.setChecked(flag)

        flag = bool(getToolCfg(EToolCfg.autoMkCtrl.name))
        self.mkCtrlCb.setChecked(flag)

        flag = bool(getToolCfg(EToolCfg.autoMkCache.name))
        self.mkCacheCb.setChecked(flag)

        flag = bool(getToolCfg(EToolCfg.fieldStyle.name))
        self.fieldStyleCb.setChecked(flag)
        
    def onCbClick(self,e):
        self.isChange = True

    def onAuthorChange(self):
        self.isChange = True
    
    def onClickSave(self):        
        nameStr = self.authorTxt.text()
        nameStr = str(nameStr).strip()
        setToolCfg(EToolCfg.author.name,nameStr,False)
        idx = self.varProGroup.buttons().index(self.proBtn)
        setToolCfg(EToolCfg.fieldPro.name,idx,False)
        val = 1 if self.autoBtnCB.isChecked() else 0
        setToolCfg(EToolCfg.autoBtn.name,val,False)

        val = 1 if self.autoCtrlCb.isChecked() else 0
        setToolCfg(EToolCfg.autoCtrl.name,val,False)

        val = 1 if self.fieldStyleCb.isChecked() else 0
        setToolCfg(EToolCfg.fieldStyle.name,val,False)

        val = 1 if self.mkProxyCb.isChecked() else 0
        setToolCfg(EToolCfg.autoMkProxy.name,val,False)

        val = 1 if self.mkCtrlCb.isChecked() else 0
        setToolCfg(EToolCfg.autoMkCtrl.name,val,False)

        val = 1 if self.mkCacheCb.isChecked() else 0
        setToolCfg(EToolCfg.autoMkCache.name,val,False)

        val = 1 if self.autoListCb.isChecked() else 0
        setToolCfg(EToolCfg.autoList.name,val,True)
        self.close()

    def onRadioBtnClick(self,btn):
        self.proBtn = btn
        self.isChange = True

    def getRadioBtn(self,text,px,py)->QRadioButton:
        privateBtn = QRadioButton(self)        
        privateBtn.setText(text)
        privateBtn.adjustSize()
        privateBtn.move(px,py)
        return privateBtn

    def getCheckBox(self,text,px,py,tips:str="")->QCheckBox:
        cb = QCheckBox(self)        
        cb.setText(text)
        cb.adjustSize()
        cb.move(px,py)
        cb.setToolTip(tips)
        return cb

    def closeEvent(self,e):        
        super().closeEvent(e)
        self.closed[bool].emit(self.isChange)
    def showEvent(self,e):
        super().showEvent(e)        
        self.isChange = False

'''
if __name__ == "__main__":      
    initVersionInf(VERSION_PATH)
    initDataTypeCfg()
    initToolCfg() 
    app = QApplication(sys.argv)    
    panel = SettingWin()
    panel.show()
    sys.exit(app.exec_())
'''
